/*
 * Created on Feb 6, 2007
 */
package org.pricejd.gui;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 */
public class Client {
	public static void main(String args[]) {
		Socket connection = null;
		PrintWriter out = null;
		BufferedReader in = null;
		
		if(args.length != 2) {
			System.err.println("Usage: Client <ip address> <port>");
			System.exit(1);
		}
		
		String host = args[0];
		int port = Integer.parseInt(args[1]);
		
		try {
			connection = new Socket(host, port);
			out = new PrintWriter(connection.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		} catch (UnknownHostException e) {
			System.err.println("Sorry, I do not know who this is: " + host);
			System.exit(1);
		} catch (IOException e) {
			System.err.println("Couldn't get I/O for the connection to: " + host);
			System.exit(1);
		}
		
		BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
		String fromUser;
		
		System.out.print("> ");
		try {
			while (! (fromUser = stdIn.readLine()).equals("quit")) {
				out.println(fromUser);
				System.out.println("   " + in.readLine());
				System.out.print("> ");
			}
		} catch (IOException e) {}
		
		try {
			out.close();
			in.close();
			stdIn.close();
			connection.close();
		} catch (IOException e) {
			
		}
	}
}
